/*
 * Sampler.java
 */

package org.sunspotworld.demo;

import com.sun.spot.util.Queue;
import com.sun.spot.util.Utils;
import java.util.Vector;

/**
 *
 * @author kkmitch
 * @revised by mi zhang
 */

public class Sampler implements SensorTypes,PacketTypes,ProtocolParameter{

    /**
     * Local Variables.
     */
    private Accelerometer acc = null;
    private AnalogPort port0 = null;
    private AnalogPort port1 = null;
    private AnalogPort port2 = null;
    private AnalogPort port3 = null;
    private Vector myDatagram = null;
    private static final int pkt_number = 10;

    public Sampler() {

        System.out.println("Sampler: Initializing.");
        acc = new Accelerometer();
        port0 = new AnalogPort(0);
        port1 = new AnalogPort(1);
        port2 = new AnalogPort(2);
        port3 = new AnalogPort(3);
        myDatagram =  new Vector();
    }

    public void sampleRun(Vector format, Queue sendQu) {

        samplingLED.setColor(green);
        for (int i = 0; i < pkt_number; i++) {
            long now = System.currentTimeMillis();
            samplingLED.setOn();
            for (int j = 0; j < format.size(); j++) {
                Byte byteObj = (Byte)format.elementAt(i);
                switch(byteObj.byteValue()){
                    case GENERIC:
                        System.out.println("SensorType: GENERIC.");
                        break;
                    case ACCELEROMETER_SENSOR_X:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_X.");
                        myDatagram.addElement(new Integer(acc.getAccX()));
                        break;
                    case ACCELEROMETER_SENSOR_Y:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_Y.");
                        myDatagram.addElement(new Integer(acc.getAccY()));
                        break;
                    case ACCELEROMETER_SENSOR_Z:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_Z.");
                        myDatagram.addElement(new Integer(acc.getAccZ()));
                        break;
                    case ACCELEROMETER_SENSOR_TILT_X:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_TILT_X.");
                        myDatagram.addElement(new Integer(acc.getTiltX()));
                        break;
                    case ACCELEROMETER_SENSOR_TILT_Y:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_TILT_Y.");
                        myDatagram.addElement(new Integer(acc.getTiltY()));
                        break;
                    case ACCELEROMETER_SENSOR_TILT_Z:
                        System.out.println("SensorType: ACCELEROMETER_SENSOR_TILT_Z.");
                        myDatagram.addElement(new Integer(acc.getTiltZ()));
                        break;
                    case TEMP_SENSOR:
                        System.out.println("SensorType: TEMP_SENSOR.");
                        break;
                    case LIGHT_SENSOR:
                        System.out.println("SensorType: LIGHT_SENSOR.");
                        break;
                    case A0:
                        System.out.println("SensorType: A0.");
                        myDatagram.addElement(new Integer(port0.getValue()));
                        break;
                    case A1:
                        System.out.println("SensorType: A1.");
                        myDatagram.addElement(new Integer(port1.getValue()));
                        break;
                    case A2:
                        System.out.println("SensorType: A2.");
                        myDatagram.addElement(new Integer(port2.getValue()));
                        break;
                    case A3:
                        System.out.println("SensorType: A3.");
                        myDatagram.addElement(new Integer(port3.getValue()));
                        break;
                    default:
                        System.out.println("Invalid SensorType.");
                        break;
                }
            }
            samplingLED.setOff();
            if (SAMPLE_PERIOD - (System.currentTimeMillis() - now) > 0) {
                System.out.println("Sampler: Sleeping.......");
                Utils.sleep(SAMPLE_PERIOD - (System.currentTimeMillis() - now));
            }
        }
     // Forming Datagram by adding headers and data
        sendQu.put(new Byte(GENERIC).toString() + new Byte(DATA_REPLY).toString() + myDatagram.toString());
        myDatagram.removeAllElements();
    }
}